package org.example.examfx02.controller;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import org.example.examfx02.entity.User;
import org.example.examfx02.tool.StringUtil;
import org.example.examfx02.tool.SystemData;

public class ChangePasswordViewController {
    SystemData sys = SystemData.getInstance();
    User user = SystemData.getCurUser();
    @FXML
    Label label;
    @FXML
    Button admitBtn;
    @FXML
    Button clearBtn;
    @FXML
    PasswordField oldPassword;
    @FXML
    PasswordField newPassword;
    @FXML
    PasswordField confirmPassword;
    public void admit(){
        if(StringUtil.isEmpty(oldPassword.getText())){
            label.setText("密码不能为空");
            label.setVisible(true);
        }
        else if(oldPassword.getText().equals(user.getPassword())){
            if(StringUtil.isEmpty(newPassword.getText())){
                label.setVisible(true);
                label.setText("新密码不能为空");
            }else if(StringUtil.isEmpty(confirmPassword.getText())){
                label.setText("请确认新密码");
                label.setVisible(true);
            }else if(newPassword.getText().equals(confirmPassword.getText())){
                user.setPassword(newPassword.getText());
                user.setChanged(true);
                System.out.println(user.getPassword());
            }else{
                label.setText("新密码不一致,请检查是否输入正确");
                label.setVisible(true);
            }
        }else{
            label.setVisible(true);
            label.setText("密码不正确！");
        }
    }
    public void clear(){
        oldPassword.clear();
        newPassword.clear();
        confirmPassword.clear();
        label.setVisible(false);
    }

}
